import path from 'path'
import fs from 'fs'
import yaml from 'yaml'
export const _dirname =
  path.resolve() + path.sep + 'plugins' + path.sep + 'Tkdream-1.2'

export async function _path(id: string) {
  try {
    const filePath = path.join(
      _dirname,
      'resources',
      'data',
      'player',
      `${id}.json`
    )
    const filecangku = path.join(
      _dirname,
      'resources',
      'data',
      'cangku',
      `${id}.json`
    )
    const filebiny = path.join(
      _dirname,
      'resources',
      'data',
      'barracks',
      `${id}.json`
    )
    const thing = path.join(_dirname, 'resources', 'data', 'item')
    const player = JSON.parse(await fs.promises.readFile(filePath, 'utf-8'))
    const bingyin = JSON.parse(await fs.promises.readFile(filebiny, 'utf-8'))
    const cangku = JSON.parse(await fs.promises.readFile(filecangku, 'utf-8'))
    return { thing, player, bingyin, cangku, filePath, filecangku, filebiny }
  } catch (err) {
    console.error(err)
    throw err
  }
}

export async function _yaml() {
  try {
    const yanldate = await yaml.parse(
      fs.readFileSync(path.join(_dirname, 'config', 'pet.yaml'), 'utf-8')
    )

    return yanldate
  } catch (err) {
    console.error(err)
    throw err
  }
}

export async function _item(id: string, path1: string) {
  try {
    const date = JSON.parse(
      await fs.promises.readFile(
        path.join((await _path(id)).thing, `${path1}.json`),
        'utf-8'
      )
    )
    return date
  } catch (err) {
    console.error(err)
    throw err
  }
}
export async function Write_player(id: string, data: any) {
  try {
    const json1 = JSON.stringify(data, null, 2)
    const filePath = path.join(
      _dirname,
      'resources',
      'data',
      'player',
      `${id}.json`
    )
    fs.writeFile(filePath, json1, err => {
      if (err) {
        console.log(err)
      }
    })
  } catch (err) {
    console.log(err)
    throw err
  }
}

export async function Write_iteam(id: string, path1: string, data: any) {
  try {
    const json1 = JSON.stringify(data, null, 2)
    fs.writeFile(
      path.join((await _path(id)).thing, `${path1}.json`),
      json1,
      err => {
        if (err) {
          console.log(err)
        }
      }
    )
  } catch (err) {
    console.log(err)
    throw err
  }
}

export async function Write_cangku(_path: string, id: string, data: any) {
  try {
    const arr = path.join(
      _dirname,
      'resources',
      'data',
      `${_path}`,
      `${id}.json`
    )
    for (let i = 0; i < data.length; i++) {
      if (data[i].num == undefined || data[i].num == null || data[i].num <= 0) {
        data.splice(i, 1)
      }
    }
    const json1 = JSON.stringify(data, null, 2)
    fs.writeFile(arr, json1, err => {
      if (err) {
        console.log(err)
      }
    })
  } catch (err) {
    console.log(err)
    throw err
  }
}

export async function add_currency(newcurrency: number, id: string) {
  const player = (await _path(id)).player
  player.currency_num = player.currency_num + newcurrency
  await Write_player(id, player)
}

export async function add_foodNum(newfoodNum: number, id: string) {
  const player = (await _path(id)).player
  player.foodNum = player.foodNum + newfoodNum
  await Write_player(id, player)
}

export async function existplayer(id: string) {
  const path1 = path.join(_dirname, 'resources', 'data', 'player', `${id}.json`)
  const user = fs.existsSync(path1)
  return user
}

export async function bound(date1: any, arr: any, id: string) {
  const result = arr.find((item: { [x: string]: any }) => item[id] == date1)

  return result
}

export async function place() {
  const path1 = path.join(_dirname, 'resources', 'data', 'item', 'place.json')
  const user = fs.existsSync(path1)
  if (user) return
  const data: any = fs.readFileSync(
    path.join(_dirname, 'config', 'place.json'),
    'utf-8'
  )
  fs.writeFile(path1, data, err => {
    if (err) {
      console.log(err)
    } else {
      console.log('地图数据初始化成功')
    }
  })
}

export { fs, path }
